/**
 * 
 */
package com.efast.dmom.aps.datahandlers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.aps.bean.Arc;
import com.efast.dmom.aps.bean.Route;
import com.efast.dmom.aps.bean.RouteQueue;
import com.efast.dmom.aps.bean.RouteStep;
import com.efast.dmom.aps.dao.RouteArcDao;
import com.efast.dmom.aps.dao.RouteQueueDao;
import com.efast.dmom.aps.dao.RouteStepDao;
import com.efast.dmom.aps.entity.RouteArcEntity;
import com.efast.dmom.aps.entity.RouteQueueEntity;
import com.efast.dmom.aps.entity.RouteStepEntity;
import com.efast.dmom.aps.util.CommonConstants;
import com.efast.dmom.aps.util.TimeUtil;

/**
 * 
 * @author Aaron Liu
 *
 *         2020年2月13日
 */
@Component
public class RouteArcDataHandler {

	@Autowired
	private RouteArcDao arcDao;
	
	@Autowired
	private RouteQueueDao queueDao;
	
	@Autowired
	private RouteStepDao routeStepDao;

	protected void saveArc(Route route, List<Arc> arcList) {

		if (route == null || arcList == null || arcList.size() <= 0) {
			return;
		}

		List<RouteStepEntity> stepEntityList = new ArrayList<RouteStepEntity>();

		for (RouteStep step : route.getRouteSteps()) {
			stepEntityList.add(step.getInnerData());
		}

		List<RouteArcEntity> arcEntityList = toInnerData(arcList);

		List<RouteQueueEntity> queueEntity = new ArrayList<RouteQueueEntity>();

		for (RouteQueue queue : route.getRouteQueues()) {
			queueEntity.add(queue.getInnerData());
		}

		for (RouteArcEntity arcEntity : arcEntityList) {

			arcEntity.setRouteId(route.getInnerData().getId());

			arcEntity.setSiteId(DataHandlerUtils.getSiteId());
			arcEntity.setCreationTime(TimeUtil.getLocalDateTime());
			arcEntity.setCreationTimeZone(TimeUtil.getLocalTimeZone());
			arcEntity.setCreator(SpringWebUtils.getRequestUser());
			arcEntity.setLastModifiedTime(TimeUtil.getLocalDateTime());
			arcEntity.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			arcEntity.setLastModifier(SpringWebUtils.getRequestUser());
			// arcEntity.setCreationPid(DataHandlerUtils.getDmomPid());
			// arcEntity.setLastModifiedPid(DataHandlerUtils.getDmomPid());
			arcEntity.setRowStatus(1);
			arcEntity.setRowTrxId(1);

			long fromNodeKey = getFromNodeKey(arcEntity, stepEntityList, queueEntity);
			arcEntity.setFromNodeId(fromNodeKey);

			long toNodeKey = getToNodeKey(arcEntity, stepEntityList, queueEntity);
			arcEntity.setToNodeId(toNodeKey);

		}

		arcDao.saveArc(arcEntityList);
	}
	
	private long getStepKey(String uuid){
		return routeStepDao.getStepKeyByUUID(uuid);
	}
	
	private long getQueueKey(String uuid){
		return queueDao.getQueueIdByUUID(uuid);
	}

	private long getFromNodeKey(RouteArcEntity arcEntity, List<RouteStepEntity> stepList,
			List<RouteQueueEntity> queueList) {

		String fromNodeType = CommonConstants.ROUTE_NODE_TYPE_STEP.equals(arcEntity.getFromNodeType()) ? "ROUTE_STEP"
				: "ROUTE_QUEUE";

		if (fromNodeType == CommonConstants.ROUTE_NODE_TYPE_STEP) {
			for (RouteStepEntity stepEntity : stepList) {
				if (stepEntity.getUuid().equals(arcEntity.getFromNodeUUID())){
					long stepId = stepEntity.getId();
					if(stepId <=0){
						return getStepKey(stepEntity.getUuid());
					}
					return stepId;
				}
			}
		} else if (fromNodeType == CommonConstants.ROUTE_NODE_TYPE_QUEUE) {
			for (RouteQueueEntity queueEntity : queueList) {
				if (queueEntity.getUuid().equals(arcEntity.getFromNodeUUID())){
					
					long queueKey = queueEntity.getId();
					if(queueKey <=0){
						return getQueueKey(queueEntity.getUuid());
					}
					return queueKey;
				}
			}
		}
		return -1;
	}

	private long getToNodeKey(RouteArcEntity arcEntity, List<RouteStepEntity> stepList,
			List<RouteQueueEntity> queueList) {

		String toNodeType = CommonConstants.ROUTE_NODE_TYPE_STEP.equals(arcEntity.getToNodeType()) ? "ROUTE_STEP"
				: "ROUTE_QUEUE";

		if (toNodeType == CommonConstants.ROUTE_NODE_TYPE_STEP) {
			for (RouteStepEntity stepEntity : stepList) {
				if (stepEntity.getUuid().equals(arcEntity.getToNodeUUID())){
					long stepId = stepEntity.getId();
					if(stepId <=0){
						return getStepKey(stepEntity.getUuid());
					}
					return stepId;
				}
			}
		} else if (toNodeType == CommonConstants.ROUTE_NODE_TYPE_QUEUE) {
			for (RouteQueueEntity queueEntity : queueList) {
				if (queueEntity.getUuid().equals(arcEntity.getToNodeUUID())){
					long queueKey = queueEntity.getId();
					if(queueKey <=0){
						return getQueueKey(queueEntity.getUuid());
					}
					return queueKey;
				}
			}
		}
		return -1;
	}

	protected void removeArcsByRouteId(long routeId) {
		arcDao.removeArcsByRouteId(routeId);
	}

	protected void removeArcs(List<Arc> arcList) {
		
		if(arcList != null && arcList.size() > 0){
			arcDao.removeArcs(toInnerData(arcList));
		}
	}

	protected List<Arc> getRouteArcByRouteId(long routeKey) {

		List<Arc> arcList = new ArrayList<Arc>();

		List<RouteArcEntity> entityList = arcDao.getRouteArcByRouteId(routeKey);

		for (RouteArcEntity arcEntit : entityList) {

			Arc beanObj = new Arc();
			beanObj.setInnerData(arcEntit);

			arcList.add(beanObj);
		}
		return arcList;
	}

	protected List<Arc> fromInnerData(List<RouteArcEntity> entityList) {

		List<Arc> arcList = new ArrayList<Arc>();

		for (RouteArcEntity arcEntity : entityList) {

			Arc arc = new Arc();
			arc.setInnerData(arcEntity);

			arcList.add(arc);
		}
		return arcList;
	}

	protected List<RouteArcEntity> toInnerData(List<Arc> arcList) {

		List<RouteArcEntity> entityList = new ArrayList<RouteArcEntity>();

		for (Arc arc : arcList) {

			entityList.add(arc.getInnerData());
		}
		return entityList;
	}
}
